package com.stack.knowyouweb.controller;

import com.google.gson.Gson;

import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoubean.bean.HomeWork;
import com.stack.knowyoubean.bean.RepJson;
import com.stack.knowyouservice.service.HomeWorkService;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * 作业表响应层
 * RestController = controller+responseBody
 * controller响应视图页面
 *
 * @author stack
 */
@RestController
@Scope("singleton")
@RequestMapping("/api")
public class HomeWorkController {

    /**
     * 基于接口的动态代理 proxy
     */
    @Resource
    private HomeWorkService homeWorkService;
    @Resource
    private Gson gson;

    /**
     * 作业列表（教师）
     *
     * @param hname   作业名
     * @param hkid    分类号
     * @param hstatus 状态
     * @param page    页码
     * @param limit   条数
     * @param session session
     * @return 作业库列表
     */
    @GetMapping("/homework")
    public String homeWorkSearch(String hname, Integer hkid, Integer hstatus, Integer page, Integer limit, HttpSession session) {
        //通过选择课程的安全保证查看作业列表的权限限制，，限制角色
        RepJson<List<HomeWork>> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            HashMap<String, Object> homeWorkMap = homeWorkService.homeworkSearch(hname, hkid, hstatus, page, limit, sid);
            return getString(repJson, homeWorkMap);
        }

    }

    /**
     * 分类查看作业列表
     *
     * @param hkid    分类号
     * @param page    页码
     * @param limit   条数
     * @param session session
     * @return 分类作业列表
     */
    @GetMapping("/homework/{hkid}")
    public String homeWorkByHkid(@PathVariable("hkid") Integer hkid, Integer page, Integer limit, HttpSession session) {
        //通过选择课程的安全保证查看作业列表的权限限制，无需限制角色，返回数据控制安全
        RepJson<List<HomeWork>> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            HashMap<String, Object> homeWorkMap = homeWorkService.homeworkSearch(null, hkid, null, page, limit, sid);
            return getString(repJson, homeWorkMap);
        }
    }

    private String getString(RepJson<List<HomeWork>> repJson, HashMap<String, Object> homeWorkMap) {
        if (homeWorkMap == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(null);
            repJson.setCount(0);
            repJson.setMsg("查询成功");
        } else {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData((List<HomeWork>) homeWorkMap.get("homeworkList"));
            repJson.setCount((Integer) homeWorkMap.get("count"));
            repJson.setMsg("查询成功");
        }
        return gson.toJson(repJson);
    }

    /**
     * 查看作业的具体信息
     *
     * @param hid 作业编号
     * @return 作业信息
     */
    @GetMapping("/homework/hid/{hid}")
    public String homeWorkInfo(@PathVariable("hid") Integer hid, HttpSession session) {
        //通过角色限制访问+通过课程号保证你只能看见你有课程信息的记录，课程号的选择权限是安全的

        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            RepJson<String> repJson = new RepJson<>();
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        }
        RepJson<HomeWork> repJson = new RepJson<>();
        HomeWork homeWork = homeWorkService.homeWorkInfo(hid, (Integer) session.getAttribute("courseid"));
        repJson.setCode(GlobalConst.SUCCESS_CODE);
        repJson.setData(homeWork);
        return gson.toJson(repJson);
    }

    /**
     * 从 session中获取课程号,选择课程时限定了只能选择你持有的课程，不存在安全问题
     *
     * @param homeWork 作业
     * @param session  session
     * @return 课程添加的标记
     */
    @PostMapping("/homework")
    public String homeworkAdd(HomeWork homeWork, HttpSession session) {
        //通过选择课程的安全保证添加作业的权限限制，限制角色
        RepJson<String> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            homeWork.setSid(sid);
            boolean b = homeWorkService.homeWorkInsert(homeWork);
            if (b) {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("添加作业成功");
                return gson.toJson(repJson);
            } else {
                repJson.setCode(GlobalConst.SUCCESS_CODE);
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("添加作业失败");
                return gson.toJson(repJson);
            }
        }
    }

    /**
     * 作业更新 存在注入hid的风险,服务端校验
     *
     * @param homeWork 作业
     * @param session  session域获取课程编号
     * @return 课程更新标记
     */
    @PutMapping("/homework")
    public String homeworkUpdate(HomeWork homeWork, HttpSession session) {
        //通过选择课程的安全保证添加作业的权限限制，限制角色
        RepJson<String> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            homeWork.setSid(sid);
            boolean b = homeWorkService.homeWorkUpdate(homeWork, sid);
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            if (b) {
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("更新作业成功");
            } else {
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("更新作业失败");
            }
            return gson.toJson(repJson);
        }
    }


    @PutMapping("/homework/batchNormal")
    public String batchNormal(Integer[] idsStr, HttpSession session) {
        //通过选择课程的安全保证添加作业的权限限制，限制角色
        RepJson<String> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            boolean b = homeWorkService.homeWorkUpdateStatus(idsStr, GlobalConst.HSTATUS_RELEASE, sid);
            return getString(repJson, b);
        }

    }

    private String getString(RepJson<String> repJson, boolean b) {
        if (b) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.SUCCESS_TYPE);
            repJson.setMsg("更新状态成功");
            return gson.toJson(repJson);
        } else {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("更新状态失败");
            return gson.toJson(repJson);
        }
    }

    @PutMapping("/homework/batchStop")
    public String batchStop(Integer[] idsStr, HttpSession session) {
        //通过选择课程的安全保证添加作业的权限限制，限制角色
        RepJson<String> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {
            boolean b = homeWorkService.homeWorkUpdateStatus(idsStr, GlobalConst.HSTATUS_END, sid);
            return getString(repJson, b);
        }

    }

    @DeleteMapping("/homework/batchDel")
    public String batchDel(Integer[] idsStr, HttpSession session) {
        //通过选择课程的安全保证添加作业的权限限制，限制角色
        RepJson<String> repJson = new RepJson<>();
        Integer sid = (Integer) session.getAttribute("courseid");
        if (sid == null) {
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            repJson.setData(GlobalConst.ERROR_TYPE);
            repJson.setMsg("没有选择课程");
            return gson.toJson(repJson);
        } else {

            boolean b = homeWorkService.homeWorkDelete(idsStr, sid);
            repJson.setCode(GlobalConst.SUCCESS_CODE);
            if (b) {
                repJson.setData(GlobalConst.SUCCESS_TYPE);
                repJson.setMsg("删除作业成功");
            } else {
                repJson.setData(GlobalConst.ERROR_TYPE);
                repJson.setMsg("删除作业失败");
            }
            return gson.toJson(repJson);
        }

    }


}
